//
//  Model3DViewController.swift
//  FlutterTest
//
//  Created by 张鑫 on 2020/8/17.
//  Copyright © 2020 www.lionaitech.com. All rights reserved.
//

import SceneKit

class Model3DViewController: UIViewController {
    
    var threeDView: SCNView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        
        loadModelFile()
        addTextureOrColor()
        
//        CADisplayLink
        
        
    }
    func loadModelFile() {
        
        //设置摄像机架设点
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        cameraNode.position = SCNVector3(0, 0, 5)
        
        //设置光源点
        let lightNode = SCNNode()
        lightNode.light = SCNLight()
        lightNode.light?.type = .omni
        lightNode.position = SCNVector3(0, 10, 10)
        
        //设置周围环境
        let ambientLightNode = SCNNode()
        ambientLightNode.light = SCNLight()
        ambientLightNode.light?.type = .ambient
        ambientLightNode.light?.color = UIColor.lightGray
        
        // 创建场景，添加各个元素
        // let scene = SCNScene(named: "art.scnassets/ship.scn")
        let scene = SCNScene(named: "art.scnassets/untitled.dae")
        // let scene = SCNScene(named: "art.scnassets/untitled.obj")
        // let url = Bundle.main.url(forResource: "untitled", withExtension: "obj")
        // let source = SCNSceneSource(url: url!, options: nil)
        // let scene = source?.scene(options: nil)
        
        scene?.rootNode.addChildNode(cameraNode)
        scene?.rootNode.addChildNode(lightNode)
        scene?.rootNode.addChildNode(ambientLightNode)
        
        threeDView = SCNView(frame: CGRect.zero)
        threeDView?.translatesAutoresizingMaskIntoConstraints = false
        threeDView?.scene = scene
        threeDView?.allowsCameraControl = true
        //        threeDView?.showsStatistics = true
        threeDView?.backgroundColor = UIColor.cyan
        view.addSubview(threeDView!)
        
        
        threeDView?.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        threeDView?.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        threeDView?.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        threeDView?.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        
        //设置屏幕视角，需要是有camera的node
        threeDView?.pointOfView
    }
    
    func addTextureOrColor() {
        
//        print("\(threeDView?.scene?.rootNode.childNodes)")
        
        let shirtNode = threeDView?.scene?.rootNode.childNode(withName: "ball", recursively: true)
        // 置空使用模型自带的纹理
        // shirtNode?.geometry?.firstMaterial?.diffuse.contents = nil
        // 设置纹理名称，使用新的纹理
         shirtNode?.geometry?.firstMaterial?.diffuse.contents = "art.scnassets/sun.jpg"
        // 也可以直接设置颜色
         shirtNode?.geometry?.firstMaterial?.diffuse.contents = UIColor.red
        // 可以设置图片对象
        // shirtNode?.geometry?.firstMaterial?.diffuse.contents = UIImage(named: "export_0_texture19.png")
        
        let material = SCNMaterial()
        material.diffuse.contents = "art.scnassets/sun.jpg"//UIColor.green
        shirtNode?.geometry?.materials = [material]
        
        
        
        
        
    }
    
    
}
